/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xdlink.component.channel.impl.bt;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;

import com.xuexiang.xdlink.util.Utils;

import java.util.UUID;

/**
 * 低功耗蓝牙特征字信息
 *
 * @author xuexiang
 * @since 2018/9/7 上午10:07
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class CharacteristicInfo {

    /**
     * BLE设备服务UUID
     */
    private String mServiceUUID;

    /**
     * BLE数据特征字服务UUID
     */
    private String mCharacteristicUUID;

    /**
     * BLE数据特征字
     */
    private BluetoothGattCharacteristic mGattCharacteristic;

    /**
     * 构造函数
     *
     * @param serviceUUID        服务UUID
     * @param characteristicUUID 特征值UUID
     */
    public CharacteristicInfo(@NonNull String serviceUUID, @NonNull String characteristicUUID) {
        mServiceUUID = serviceUUID;
        mCharacteristicUUID = characteristicUUID;
    }

    public String getServiceUUID() {
        return mServiceUUID;
    }

    public CharacteristicInfo setServiceUUID(@NonNull String serviceUUID) {
        mServiceUUID = serviceUUID;
        return this;
    }

    public String getCharacteristicUUID() {
        return mCharacteristicUUID;
    }

    public CharacteristicInfo setCharacteristicUUID(@NonNull String characteristicUUID) {
        mCharacteristicUUID = characteristicUUID;
        return this;
    }

    public BluetoothGattCharacteristic getGattCharacteristic() {
        Utils.requireNonNull(mGattCharacteristic, "GattCharacteristic cannot be null!You must call setGattCharacteristic method first!");
        return mGattCharacteristic;
    }

    public CharacteristicInfo setGattCharacteristic(BluetoothGatt bluetoothGatt) {
        BluetoothGattService gattService = bluetoothGatt.getService(UUID.fromString(mServiceUUID));
        mGattCharacteristic = gattService.getCharacteristic(UUID.fromString(mCharacteristicUUID));
        return this;
    }

    public CharacteristicInfo setGattCharacteristic(BluetoothGattCharacteristic gattCharacteristic) {
        mGattCharacteristic = gattCharacteristic;
        return this;
    }

    /**
     * 服务UUID是否相等
     *
     * @param characteristicInfo
     * @return 服务UUID是否相等
     */
    public boolean isServiceEquals(@NonNull CharacteristicInfo characteristicInfo) {
        return mServiceUUID.equals(characteristicInfo.getServiceUUID());
    }

    /**
     * 是否是相同的特征字
     *
     * @param characteristic
     * @return 是否是相同的特征字
     */
    public boolean isCharacteristicEquals(@NonNull BluetoothGattCharacteristic characteristic) {
        return mCharacteristicUUID.equals(characteristic.getUuid().toString());
    }
}
